<?php
/**
 * Created by PhpStorm.
 * User: laowang
 * Date: 2018/4/21
 * Time: 下午3:52
 */

namespace app\home\controller;
use app\home\model\WechatUser;
use app\home\model\Xuexiao;
class Ranks extends Base
{
    public function index(){
        $school_id = input('id');
        $school = Xuexiao::where('dx_id',$school_id)->find();
        $wechatModel = new WechatUser();
        $this->jssdk();
        $all = $wechatModel->where(['school_id' => $school_id,'subscribe' => 1,'review_status' => 1,'role' => 0,'rank' => ['neq',0]])->order('rank')->field('nickname,name,headimgurl,rank,percent,order_id,sign_ex,sign_dx')->limit(30)->select();
        $alls = $wechatModel->where(['school_id' => $school_id,'subscribe' => 1,'review_status' => 1,'role' => 0,'ranks' => ['neq',0]])->order('ranks')->field('nickname,name,headimgurl,ranks,percents,order_id,summary_ex,summary_dx')->limit(30)->select();
        $allss = $wechatModel->where(['school_id' => $school_id,'subscribe' => 1,'review_status' => 1,'role' => 0,'rankss' => ['neq',0]])->order('rankss')->field('nickname,name,headimgurl,rankss,test_dx,test_ex,order_id')->limit(30)->select();
        //分享图片及链接及描述
        $this->assign('sign',$all);
        $this->assign('journal',$alls);
        $this->assign('test',$allss);
        $before = date('Y')."/".date('m')."/1";
        if (date('n') == 1){
            if (date('d') == 1){
                // 新年伊始 1月1号
                $before = (date('Y')-1)."/12/1";
            }
        }else{
            if (date('d') == 1){
                // 每月1号
                $before = date('Y')."/0".(date('n')-1)."/1";
            }
        }
        if(time() > strtotime(date('Y-m-d 03:35:00'))){
            $time = time()-3600*24;
        }else{
            $time = time()-3600*24*2;
        }
        $this->assign('time',$time);
        $this->assign('school_id',$school_id);
        $this->assign('before',$before);
        $this->assign('d_name',iconv("gbk", "utf-8//ignore", $school['dx']));
        return $this->fetch();
    }
    public function more(){
        $wechatModel = new WechatUser();
        $len = input('length');
        $school_id = input('school_id');
        $type = input('type'); // 类型 1 签到 2 总结 3 测试
        switch ($type){
            case 1:
                $all = $wechatModel->where(['school_id' => $school_id,'subscribe' => 1,'review_status' => 1,'role' => 0,'rank' => ['neq',0]])->order('rank')->field('nickname,name,headimgurl,rank,percent,order_id,sign_ex,sign_dx')->limit($len,30)->select();
                break;
            case 2:
                $all = $wechatModel->where(['school_id' => $school_id,'subscribe' => 1,'review_status' => 1,'role' => 0,'ranks' => ['neq',0]])->order('ranks')->field('nickname,name,headimgurl,ranks,percents,order_id,summary_ex,summary_dx')->limit($len,30)->select();
                break;
            default:
                $all = $wechatModel->where(['school_id' => $school_id,'subscribe' => 1,'review_status' => 1,'role' => 0,'rankss' => ['neq',0]])->order('rankss')->field('nickname,name,headimgurl,rankss,order_id,test_ex,test_dx')->limit($len,30)->select();
                break;
        }
        if ($all){
            foreach ($all as $value){
                $value['name'] = substr($value['name'],0,3)."同学";
                $value['haha'] = $len;
            }
            return $this->success('加载成功','',$all);
        }else{
            return $this->error('加载失败','',$all);
        }
    }
}